package cn.gmph.blog.service.post.dao.helper;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.gmph.blog.service.post.entity.PostsDO;
import cn.gmph.blog.web.dto.QueryDTO;
import cn.gmph.core.utils.StringUtils;

public class PostsProvider {

    private Logger              logger     = LoggerFactory.getLogger(this.getClass());

    private static final String TABLE_NAME = "wp_posts";

    public String update(Map<String, Object> params) {
        PostsDO posts = (PostsDO) params.get("posts");
        return new SQL() {
            {
                UPDATE(TABLE_NAME);
                if (posts.getPostAuthor() != null) {
                    SET("post_author=#{posts.postAuthor}");
                }
                if (posts.getPostDate() != null) {
                    SET("post_date=#{posts.postDate}");
                }
                if (posts.getPostDateGmt() != null) {
                    SET("post_date_gmt=#{posts.postDateGmt}");
                }
                if (StringUtils.isNotBlank(posts.getPostContent())) {
                    SET("post_content=#{posts.postContent}");
                }
                if (StringUtils.isNotBlank(posts.getPostTitle())) {
                    SET("post_title=#{posts.postTitle}");
                }
                if (StringUtils.isNotBlank(posts.getPostExcerpt())) {
                    SET("post_excerpt=#{posts.postExcerpt}");
                }
                if (StringUtils.isNotBlank(posts.getPostStatus())) {
                    SET("post_status=#{posts.postStatus}");
                }
                if (StringUtils.isNotBlank(posts.getCommentStatus())) {
                    SET("comment_status=#{posts.commentStatus}");
                }
                if (StringUtils.isNotBlank(posts.getPingStatus())) {
                    SET("ping_status=#{posts.pingStatus}");
                }
                if (StringUtils.isNotBlank(posts.getPostPassword())) {
                    SET("post_password=#{posts.postPassword}");
                }
                if (StringUtils.isNotBlank(posts.getPostName())) {
                    SET("post_name=#{posts.postName}");
                }
                if (StringUtils.isNotBlank(posts.getToPing())) {
                    SET("to_ping=#{posts.toPing}");
                }
                if (StringUtils.isNotBlank(posts.getPinged())) {
                    SET("pinged=#{posts.pinged}");
                }
                if (posts.getPostModified() != null) {
                    SET("post_modified=#{posts.postModified}");
                }
                if (posts.getPostModifiedGmt() != null) {
                    SET("post_modified_gmt=#{posts.postModifiedGmt}");
                }
                if (StringUtils.isNotBlank(posts.getPostContentFiltered())) {
                    SET("post_content_filtered=#{posts.postContentFiltered}");
                }
                if (posts.getPostParent() != null) {
                    SET("post_parent=#{posts.postParent}");
                }
                if (StringUtils.isNotBlank(posts.getGuid())) {
                    SET("guid=#{posts.guid}");
                }
                if (posts.getMenuOrder() != null) {
                    SET("menu_order=#{posts.menuOrder}");
                }
                if (StringUtils.isNotBlank(posts.getPostType())) {
                    SET("post_type=#{posts.postType}");
                }
                if (StringUtils.isNotBlank(posts.getPostMimeType())) {
                    SET("post_mime_type=#{posts.postMimeType}");
                }
                if (posts.getCommentCount() != null) {
                    SET("comment_count=#{posts.commentCount}");
                }
                WHERE("id = #{posts.id}");
            }
        }.toString();
    }

    public String pageList(Map<String, Object> params) {
        PostsDO posts = (PostsDO) params.get("posts");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("*");
                FROM(TABLE_NAME);
                if (posts.getId() != null) {
                    WHERE("ID=#{posts.id}");
                }
                if (posts.getPostAuthor() != null) {
                    WHERE("post_author=#{posts.postAuthor}");
                }
                if (posts.getPostDate() != null) {
                    WHERE("post_date=#{posts.postDate}");
                }
                if (posts.getPostDateGmt() != null) {
                    WHERE("post_date_gmt=#{posts.postDateGmt}");
                }
                if (StringUtils.isNotBlank(posts.getPostContent())) {
                    WHERE("post_content=#{posts.postContent}");
                }
                if (StringUtils.isNotBlank(posts.getPostTitle())) {
                    WHERE("post_title=#{posts.postTitle}");
                }
                if (StringUtils.isNotBlank(posts.getPostExcerpt())) {
                    WHERE("post_excerpt=#{posts.postExcerpt}");
                }
                if (StringUtils.isNotBlank(posts.getPostStatus())) {
                    WHERE("post_status=#{posts.postStatus}");
                }
                if (StringUtils.isNotBlank(posts.getCommentStatus())) {
                    WHERE("comment_status=#{posts.commentStatus}");
                }
                if (StringUtils.isNotBlank(posts.getPingStatus())) {
                    WHERE("ping_status=#{posts.pingStatus}");
                }
                if (StringUtils.isNotBlank(posts.getPostPassword())) {
                    WHERE("post_password=#{posts.postPassword}");
                }
                if (StringUtils.isNotBlank(posts.getPostName())) {
                    WHERE("post_name=#{posts.postName}");
                }
                if (StringUtils.isNotBlank(posts.getToPing())) {
                    WHERE("to_ping=#{posts.toPing}");
                }
                if (StringUtils.isNotBlank(posts.getPinged())) {
                    WHERE("pinged=#{posts.pinged}");
                }
                if (posts.getPostModified() != null) {
                    WHERE("post_modified=#{posts.postModified}");
                }
                if (posts.getPostModifiedGmt() != null) {
                    WHERE("post_modified_gmt=#{posts.postModifiedGmt}");
                }
                if (StringUtils.isNotBlank(posts.getPostContentFiltered())) {
                    WHERE("post_content_filtered=#{posts.postContentFiltered}");
                }
                if (posts.getPostParent() != null) {
                    WHERE("post_parent=#{posts.postParent}");
                }
                if (StringUtils.isNotBlank(posts.getGuid())) {
                    WHERE("guid=#{posts.guid}");
                }
                if (posts.getMenuOrder() != null) {
                    WHERE("menu_order=#{posts.menuOrder}");
                }
                if (StringUtils.isNotBlank(posts.getPostType())) {
                    WHERE("post_type=#{posts.postType}");
                }
                if (StringUtils.isNotBlank(posts.getPostMimeType())) {
                    WHERE("post_mime_type=#{posts.postMimeType}");
                }
                if (posts.getCommentCount() != null) {
                    WHERE("comment_count=#{posts.commentCount}");
                }
                ORDER_BY("id desc limit " + start + ", " + limit);
            }
        }.toString();
    }

    public String pageListCount(Map<String, Object> params) {
        PostsDO posts = (PostsDO) params.get("posts");
        return new SQL() {
            {
                SELECT("count(1)");
                FROM(TABLE_NAME);
                if (posts.getId() != null) {
                    WHERE("ID=#{posts.id}");
                }
                if (posts.getPostAuthor() != null) {
                    WHERE("post_author=#{posts.postAuthor}");
                }
                if (posts.getPostDate() != null) {
                    WHERE("post_date=#{posts.postDate}");
                }
                if (posts.getPostDateGmt() != null) {
                    WHERE("post_date_gmt=#{posts.postDateGmt}");
                }
                if (StringUtils.isNotBlank(posts.getPostContent())) {
                    WHERE("post_content=#{posts.postContent}");
                }
                if (StringUtils.isNotBlank(posts.getPostTitle())) {
                    WHERE("post_title=#{posts.postTitle}");
                }
                if (StringUtils.isNotBlank(posts.getPostExcerpt())) {
                    WHERE("post_excerpt=#{posts.postExcerpt}");
                }
                if (StringUtils.isNotBlank(posts.getPostStatus())) {
                    WHERE("post_status=#{posts.postStatus}");
                }
                if (StringUtils.isNotBlank(posts.getCommentStatus())) {
                    WHERE("comment_status=#{posts.commentStatus}");
                }
                if (StringUtils.isNotBlank(posts.getPingStatus())) {
                    WHERE("ping_status=#{posts.pingStatus}");
                }
                if (StringUtils.isNotBlank(posts.getPostPassword())) {
                    WHERE("post_password=#{posts.postPassword}");
                }
                if (StringUtils.isNotBlank(posts.getPostName())) {
                    WHERE("post_name=#{posts.postName}");
                }
                if (StringUtils.isNotBlank(posts.getToPing())) {
                    WHERE("to_ping=#{posts.toPing}");
                }
                if (StringUtils.isNotBlank(posts.getPinged())) {
                    WHERE("pinged=#{posts.pinged}");
                }
                if (posts.getPostModified() != null) {
                    WHERE("post_modified=#{posts.postModified}");
                }
                if (posts.getPostModifiedGmt() != null) {
                    WHERE("post_modified_gmt=#{posts.postModifiedGmt}");
                }
                if (StringUtils.isNotBlank(posts.getPostContentFiltered())) {
                    WHERE("post_content_filtered=#{posts.postContentFiltered}");
                }
                if (posts.getPostParent() != null) {
                    WHERE("post_parent=#{posts.postParent}");
                }
                if (StringUtils.isNotBlank(posts.getGuid())) {
                    WHERE("guid=#{posts.guid}");
                }
                if (posts.getMenuOrder() != null) {
                    WHERE("menu_order=#{posts.menuOrder}");
                }
                if (StringUtils.isNotBlank(posts.getPostType())) {
                    WHERE("post_type=#{posts.postType}");
                }
                if (StringUtils.isNotBlank(posts.getPostMimeType())) {
                    WHERE("post_mime_type=#{posts.postMimeType}");
                }
                if (posts.getCommentCount() != null) {
                    WHERE("comment_count=#{posts.commentCount}");
                }
            }
        }.toString();
    }

    public String selectLastPosts(Map<String, Object> params) {
        QueryDTO posts = (QueryDTO) params.get("posts");
        Integer pageNum = posts.getPageNo();
        Integer pageSize = posts.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("posts.ID");
                FROM(TABLE_NAME + " posts");
                WHERE("posts.post_type =  'post'");
                WHERE("posts.post_type =  'post'");
                WHERE("posts.post_status =  'publish'");
                ORDER_BY("posts.post_date DESC  limit " + start + ", " + limit);
            }
        }.toString();
    }

    public String selectHotPosts(Map<String, Object> params) {
        QueryDTO posts = (QueryDTO) params.get("posts");
        Integer pageNum = posts.getPageNo();
        Integer pageSize = posts.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("posts.ID");
                FROM(TABLE_NAME + " posts");
                WHERE("posts.post_type =  'post'");
                WHERE("posts.post_type =  'post'");
                WHERE("posts.post_status =  'publish'");
                ORDER_BY("posts.post_date DESC  limit 0, 5");
            }
        }.toString();
    }

    public String selectRandomPosts(Map<String, Object> params) {
        QueryDTO posts = (QueryDTO) params.get("posts");
        Integer pageNum = posts.getPageNo();
        Integer pageSize = posts.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("t1.ID");
                FROM(TABLE_NAME + " AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM " + TABLE_NAME + ")-(SELECT MIN(id) FROM " + TABLE_NAME + "))+(SELECT MIN(id) FROM " + TABLE_NAME
                     + ")) AS id) AS t2");
                WHERE("t1.id >= t2.id");
                ORDER_BY("t1.id LIMIT 1");
            }
        }.toString();
    }
    
    public String selectNextPost(Map<String, Object> params) {
        QueryDTO posts = (QueryDTO) params.get("posts");
        Integer pageNum = posts.getPageNo();
        Integer pageSize = posts.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("posts.ID");
                FROM(TABLE_NAME + " posts");
                WHERE("posts.ID>#{posts.id}");
                ORDER_BY("posts.id LIMIT 1 ");
            }
        }.toString();
    }
    public String selectPrevPost(Map<String, Object> params) {
        QueryDTO posts = (QueryDTO) params.get("posts");
        Integer pageNum = posts.getPageNo();
        Integer pageSize = posts.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("posts.ID");
                FROM(TABLE_NAME + " posts");
                WHERE("posts.ID < #{posts.id}");
                ORDER_BY("posts.id LIMIT 1 ");
            }
        }.toString();
    }
}
